/*
-----------------------------------------------------------------------------
This source file is part of SapphireEngine

SapphireEngine is a Framework for Turn Based Strategy games.
For more information see http://code.google.com/p/sapphire-engine/

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.

-----------------------------------------------------------------------------
*/
#ifndef _LOADING_SCREEN_H
#define _LOADING_SCREEN_H

#include "SapphirePrerequisites.h"

#include <CEGUI/CEGUI.h>
#include <OIS/OIS.h>
#include <OgreCEGUIRenderer.h>
#include <list>

#define IMAGE "bkg.jpg"

namespace SapphireEngine
{
	namespace GUI
	{
		class LoadingScreen : public Screen
		{
		public:
			LoadingScreen(CEGUI::String layoutFile):Screen(layoutFile),mDoneLoading(false)
			{
				//Progress Bar
				mProgressBar = static_cast<ProgressBar*>(getWindowManager()->getWindow((CEGUI::utf8*)"Sapphire/Intro/LoadingBar"));
				mStepSize=0.75f;

				/* StaticImage */
				ImagesetManager::getSingleton().createImagesetFromImageFile("LoadImage", IMAGE);
				mImage = getWindowManager()->getWindow("Sapphire/Intro/Image");
				mImage->setProperty("Image", "set:LoadImage image:full_image"); // "full_image" is a default name from CEGUIImageset::Imageset()

			}
			// Events
			void subscribeEvents()
			{
				CEGUI::Event::Connection connect;

		//		CEGUI::Window *wnd = getWindowManager()->getWindow((CEGUI::utf8*)"Sapphire/Intro");
		// 		connect = wnd->subscribeEvent(CEGUI::Window::EventActivated,CEGUI::Event::Subscriber(&LoadingScreen::_activate, this));
		// 		mEventConnections.push_front(connect);

			}
			bool update(const FrameEvent &evt)
			{
				setStepSize(mStepSize*evt.timeSinceLastEvent);
				step();
				if(getProgress()>=0.975f)
					if (!mDoneLoading)
						setProgress(0.01f);
				return mContinue;
			}
			bool _activate(const CEGUI::EventArgs & arg)
			{
				return true;
			}
			void setDoneLoading()
			{
				Window *wnd = getWindowManager()->getWindow((CEGUI::utf8*)"Sapphire/Intro/LoadingBar/Lbl");
				wnd->setText("Done Loading, Press any key to continue");
				//mNextScreenName="MainMenu";
				mStepSize=100.0f;
				mDoneLoading=true;
			}
			// MouseListener
			bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
			{
				if (mDoneLoading)
					mNextScreenName="MainMenu";
				return true;
			}
			// KeyListener
			bool keyPressed(const OIS::KeyEvent &arg)
			{
		 		if (mDoneLoading)
		 			mNextScreenName="MainMenu";
				return true;
			}
			//Progress Bar
			void setProgress(float num)
			{
				mProgressBar->setProgress(num);
			}
			void setStepSize(float num)
			{
				mProgressBar->setStepSize(num);
			}
			void step()
			{
				mProgressBar->step();
			}
			void adjustProgress(float num)
			{
				mProgressBar->adjustProgress(num);
			}
			float getProgress()
			{ 
				return mProgressBar->getProgress();
			}
		private:
			ProgressBar *mProgressBar;
			float mStepSize;
			Window *mImage;
			bool mDoneLoading;
		};
	}
}

#endif